package com.nhc.activity_fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.astrob.model.LonLat;
import com.astrob.nhcmap.R;
import com.astrob.util.CoordChange;
import com.astrob.util.SunRiseSet;
import com.astrob.util.SysUtils;
import com.astrob.util.Utils;

import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.ui.DatePickerWindow;
import zuo.biao.library.ui.EditTextInfoWindow;
import zuo.biao.library.util.TimeUtil;

public class RiyueActivity extends BaseActivity implements View.OnClickListener {
    private final String TAG = "AboutActivity";

    //启动方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    /**
     * 启动这个Activity的Intent
     *
     * @param context
     * @return
     */
    public static Intent createIntent(Context context) {
        return new Intent(context, RiyueActivity.class);
    }

    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.riyue_activity);

        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>

    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    private TextView tv_lat;
    private TextView tv_datetime;
    private TextView tv_suntime_0;
    private TextView tv_suntime_1;
    private TextView tv_moontime_0;
    private TextView tv_moontime_1;


    @Override
    public void initView() {//必须调用
        tv_lat = (TextView) findViewById(R.id.tv_lat);

        tv_datetime = (TextView) findViewById(R.id.tv_datetime);
        tv_suntime_0 = (TextView) findViewById(R.id.tv_suntime_0);
        tv_suntime_1 = (TextView) findViewById(R.id.tv_suntime_1);
        tv_moontime_0 = (TextView) findViewById(R.id.tv_moontime_0);
        tv_moontime_1 = (TextView) findViewById(R.id.tv_moontime_1);

    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    LonLat _ll;
    long _date = System.currentTimeMillis();

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initData() {//必须调用
        _ll = SysUtils.getInstance().getLL();
        refreshData();
    }

    public void refreshData() {

        tv_lat.setText(SysUtils.getInstance().getLLStr(_ll));

        tv_datetime.setText(SysUtils.getInstance().getDateStr(_date));

        tv_suntime_0.setText(getString(R.string.rcrlsj) + SunRiseSet.getSunrise(_ll, _date));
        tv_suntime_1.setText(getString(R.string.rlsj) + SunRiseSet.getSunset(_ll, _date));

        tv_moontime_0.setText("月出时间   " + SunRiseSet.getSunset(_ll, _date));
        tv_moontime_0.setText("月落时间   " + SunRiseSet.getSunrise(_ll, _date));
    }

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initEvent() {//必须调用
        findViewById(R.id.ctrl_0).setOnClickListener(this);
        findViewById(R.id.ctrl_1).setOnClickListener(this);
    }
    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    //类相关监听<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ctrl_0:
                editLonLat();
                break;
            case R.id.ctrl_1:
                selectDate();
                break;
            default:
                break;
        }
    }

    private int[] CTRL_IDS = {
            R.id.ctrl_0, R.id.ctrl_1};

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            default:
                break;
            case 21://left
            case 22://right
            case 19://up
            case 20://down
            case 23://ok
                setFoucseCtrl();
                break;
        }
        return super.onKeyUp(keyCode, event);
    }

    boolean _isFirst = true;

    private void setFoucseCtrl() {
        if (!_isFirst) {
            return;
        }

        _isFirst = false;

        for (int i = 0; i < CTRL_IDS.length; i++) {
            findViewById(CTRL_IDS[i]).setFocusable(true);
        }
    }

    private int[] selectedDate = new int[]{2016, 0, 1};

    private void selectDate() {
        toActivity(DatePickerWindow.createIntent(this, new int[]{2016, 0, 1}, new int[]{2030, 0, 1}
                , TimeUtil.getDateDetail(System.currentTimeMillis())), REQUEST_TO_DATE_PICKER, false);
    }

    private void editLonLat() {
        intent = EditLonLatActivity.createIntent(context
                , _ll);
        toActivity(intent, REQUEST_TO_EDIT_LONLAT_INFO);
    }

    private final int REQUEST_TO_EDIT_LONLAT_INFO = 25;
    private final int REQUEST_TO_DATE_PICKER = 35;

    //类相关监听>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            default:
                break;
            case REQUEST_TO_EDIT_LONLAT_INFO:
                if (data != null) {
                    LonLat llA = null;
                    int coord_mode = SysUtils.getInstance().coord_mode;
                    int coord_format = SysUtils.getInstance().coord_format;
                    if (coord_format > 2) {
                        String strLL = data.getStringExtra(EditTextInfoWindow.RESULT_VALUE);
                        llA = CoordChange.getInstance().change2LL(strLL, coord_mode, coord_format);
                    } else {
                        llA = (LonLat) data.getSerializableExtra(EditTextInfoWindow.RESULT_VALUE);
                        llA = CoordChange.getInstance().getLL(llA.GetLon(), llA.GetLat(), 0, "", coord_mode, coord_format);
                    }
                    if (llA != null) {
                        _ll = llA;
                        refreshData();
                    }
                }
                break;
            case REQUEST_TO_DATE_PICKER:
                if (data != null) {
                    ArrayList<Integer> list = data.getIntegerArrayListExtra(DatePickerWindow.RESULT_DATE_DETAIL_LIST);
                    if (list != null && list.size() >= 3) {
                        selectedDate = new int[list.size()];
                        for (int i = 0; i < list.size(); i++) {
                            selectedDate[i] = list.get(i);
                        }
//                        String strDate = selectedDate[0] + "-" + (selectedDate[1]) + "-" + selectedDate[2];
                        Date dateNew = new Date(selectedDate[0] - 1900, selectedDate[1], selectedDate[2]);
                        _date = dateNew.getTime();
                        refreshData();
                    }
                }
                break;
        }
    }

    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


}
